import axios from 'axios'
//在 axios 请求拦截器里面
import qs from 'qs'
axios.interceptors.request.use(request => {
    if (request.method === 'get') {
        request.paramsSerializer = function(params) {
        return qs.stringify(params, { arrayFormat: 'comma' })
    }
    }
    return request
},(error) =>{
    return Promise.reject(error);
})
axios.defaults.baseURL = 'http://192.168.200.240:8250/'
axios.defaults.baseURL = '/api'
axios.interceptors.request.use(config => {
    config.headers.Authorization = window.sessionStorage.getItem('token');
    return config
})
//let token = window.sessionStorage.getItem('token');
function req(method, url, params, data) {
    let p = new Promise((resolve, reject) => {
        axios({
            method: method,
            url: url,
            params: params,
            //headers: { token: token },
            data: data
        }).then(function (response) {
            //if (response.data.errCode == 10000) {
            resolve(response.data);
            // } else {
            //     reject(response.data)
            // }
        }).catch(function (error) {
            reject(error);
        });
    })
    return p;
}

function req1(method, url, params, data, responseType) {
    let p = new Promise((resolve, reject) => {
        axios({
            method: method,
            url: url,
            params: params,
            //headers: '',
            data: data,
            responseType: responseType
        }).then(function (response) {
            //if (response.data.errCode == 10000) {
            resolve(response.data);
            // } else {
            //     reject(response.data)
            // }
        }).catch(function (error) {
            reject(error);
        });
    })
    return p;
}

export default {
    req,
    req1,
    get: function (url, params) {
        return req('get', url, params, null);
    },
    get1: function (url, params) {
        return req1('get', url, params, null, 'blob');
    },
    post: function (url, data) {
        return req('post', url, null, data);
    },
    delete: function (url) {
        return req('delete', url, null, null);
    },
    put: function (url, data) {
        return req('put', url, null, data);
    }
}
